<?php
apf_require_controller("Abstract");
apf_require_class('Bll_Portal_Message');

class Portal_Home_ActiveController extends AbstractController {

    public function handle_request() {
        $this->bll = new Bll_Portal_Message();
        $date = date('YmdHis');
        $date ='20130115155959';
        $end   = '20130115145959';
        $data['is_on'] = $date>=$end ? 1 : 0;
        $this->uid = $this->get_user_id();

        $data['my_msg'] = $this->bll->get_message($this->uid);
        //计算员工所在的位置
        $data['user_list'] =  $this->_get_user_list();
        if(!empty($data['my_msg'])){//如果自已填写了，需要指定到特定的位置
            if(empty($data['user_list'][544][2])){//说明这个位置没有人使用
                $data['user_list'][544][2] = $this->uid;
                $data['user_list'][$this->pos_key][2] = 0;
            }elseif($data['user_list'][544][2]!=$this->uid){
                $old_uid= $data['user_list'][544][2];
                $data['user_list'][544][2] = $this->uid;
                $data['user_list'][$this->pos_key][2] = $old_uid;
            }
        }
        $this->request->set_attribute('data', $data);
        return "Portal_Home_Active";
    }

    private function _get_user_list(){
        $user_list = $this->bll->get_all_messages();
        $has = 0;
        if(!empty($user_list)){
            $has = count($user_list);
        }
        $w=48;
        $h=28;
        $pos = array();
        for ($i=1;$i<$h;$i++){
            for($j=1;$j<$w;$j++){
                $left = 10+($j-1)*20;
                $top = 10+($i-1)*20;
                $pos[] = array($i,$j,0,$left,$top);
            }
        }
        if($has){
            $rand_pos = array_rand($pos,$has);
            if(is_numeric($rand_pos)){
                $rand_pos = array($rand_pos);
            }
            foreach($rand_pos as $k=>$pos_key){
                if($user_list[$k]['user_id']==$this->uid){
                    $this->pos_key = $pos_key;//我原本的位置
                }
                $pos[$pos_key][2] = $user_list[$k]['user_id'];
            }
        }
        return $pos;
    }
}